
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/15/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.yfoutsourcecontarct.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.application.yfoutsourcecontarct.entity.YfOutsourceContract;
import com.application.yfoutsourcecontarct.struts.form.YfOutsourceContractForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:YfOutsourceContractDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/15/2012
 * 
 */
public class YfOutsourceContractDAO extends
		HibernateGenericDao<YfOutsourceContract> {
	private static final Logger logger = Logger
			.getLogger(YfOutsourceContractDAO.class);

	private YfOutsourceContract yfOutsourceContract;

	public YfOutsourceContract getYfOutsourceContract() {
		return this.yfOutsourceContract;
	}

	public void setYfOutsourceContract(YfOutsourceContract yfOutsourceContract) {
		this.yfOutsourceContract = yfOutsourceContract;
	}

	public Integer getAllCountYfOutsourceContract(
			YfOutsourceContractForm yfOutsourceContractForm) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT COUNT(0) FROM yf_outsource_contract t left join yf_project p on t.project_no = p.project_no left join department b on p.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfOutsourceContractForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}

	public void deleteYfOutsourceContracts(
			YfOutsourceContractForm yfOutsourceContractForm) {
		String hql = " delete from YfOutsourceContract where id = '" + yfOutsourceContractForm.getId() + "'";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfOutsourceContracts failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchYfOutsourceContract(
		YfOutsourceContractForm yfOutsourceContractForm, PageBean pageBean) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT t.*,p.*,b.name project_org_name FROM yf_outsource_contract t left join yf_project p on t.project_no = p.project_no left join department b on p.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfOutsourceContractForm, sql, args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean, sql.toString(), args);
	}

	public  String getMaxOutNo(String pix ){
		pix = pix+"_外协";
		String temp = "00";
		StringBuffer buf = new StringBuffer();
		buf.append("select max(OUTSOURCE_NO) maxNo from yf_outsource_contract where OUTSOURCE_NO like '%" + pix + "%' ");
		Map projectNoMap = getJdbcTemplate().queryForMap(buf.toString());
		String maxNo = (String) projectNoMap.get("maxNo");
		System.out.println("maxNo=" + maxNo);
        if(StringUtils.isBlank(maxNo)){
        	return pix + "01";
        }
        maxNo = maxNo.substring(11);
        int no = Integer.parseInt(maxNo) + 1;
        maxNo = String.valueOf(no);
        maxNo = temp.substring(maxNo.length()) + maxNo;
		return pix + maxNo;
	}
	
	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(
			YfOutsourceContractForm yfOutsourceContractForm, StringBuffer buf,
			List args) {
		/*
		 * 需要加入查询条件时封装
		 */
		if (StringUtils.isNotBlank(yfOutsourceContractForm.getContractName())) {
			buf.append(" and t.CONTRACT_NAME like ? ");
			args.add("%" + yfOutsourceContractForm.getContractName().trim()
					+ "%");
		}
		if (StringUtils.isNotBlank(yfOutsourceContractForm.getOutsourceNo())) {
			buf.append(" and t.OUTSOURCE_NO like ? ");
			args.add("%" + yfOutsourceContractForm.getOutsourceNo().trim()
					+ "%");
		}
		if (StringUtils.isNotBlank(yfOutsourceContractForm.getProjectNo())) {
			buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfOutsourceContractForm.getProjectNo().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfOutsourceContractForm.getAssumeUnit())) {
			buf.append(" and t.ASSUME_UNIT like ? ");
			args.add("%" + yfOutsourceContractForm.getAssumeUnit().trim()
							+ "%");
		}

		if (null != yfOutsourceContractForm.getAuditStatus()
				&& yfOutsourceContractForm.getAuditStatus().longValue() != 0) {
			buf.append(" and t.AUDIT_STATUS = ? ");
			args.add(yfOutsourceContractForm.getAuditStatus());
		}
		if (StringUtils.isNotBlank(yfOutsourceContractForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfOutsourceContractForm.getId());
		}
		if (StringUtils.isNotBlank(yfOutsourceContractForm.getProjectName())) {
			buf.append(" and p.project_name like ? ");
			args.add("%" + yfOutsourceContractForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfOutsourceContractForm.getProjectOrg())) {
			buf.append(" and p.project_org = ? ");
			args.add(yfOutsourceContractForm.getProjectOrg());
		}
		if (null != yfOutsourceContractForm.getProjectType()&& yfOutsourceContractForm.getProjectType().longValue() != 0) {
			buf.append(" and p.project_type = ? ");
			args.add(yfOutsourceContractForm.getProjectType());
		}
		if (StringUtils.isBlank(yfOutsourceContractForm.getId())) {
			buf = CommonUtil.filterData(buf,args);
		}
		
		return buf;
	}

}
